﻿@if (!isLogin)
{
    <Loading />
}
else
{
    @ChildContent
}

@code {
    /// <summary>
    /// 展示内容
    /// </summary>
    [Parameter]
    public RenderFragment ChildContent { get; set; }

    /// <summary>
    /// 是否登录
    /// </summary>
    private bool isLogin { get; set; }

    /// <summary>
    /// 初始化
    /// </summary>
    /// <returns></returns>
    protected override async Task OnInitializedAsync()
    {
        var token = await Common.GetStorageAsync("token");
        Http.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}");

        if (string.IsNullOrEmpty(token))
        {
            isLogin = false;
            await Common.NavigateTo("/");
        }
        else
        {
            var result = await Http.GetFromJsonAsync<ServiceResult>($"/auth?token={token}");
            if (result.Success)
            {
                isLogin = true;
            }
            else
            {
                isLogin = false;
                await Common.NavigateTo("/");
            }
        }
    }
}